<template>
    <div class="continer" v-loading="upimgLoding">
        <div class="topbox">
            <div>
                <el-button type="primary" @click="openWord">新增</el-button>
                <el-button icon="el-icon-refresh" @click="getList">刷新列表</el-button>
            </div>
            <el-form :model="search" :inline="true" ref="sc" @submit.native.prevent>
                <el-form-item label="" prop="name">
                    <el-input type="text" v-model="search.name" placeholder="请输入姓名"></el-input>
                </el-form-item>
                <el-button type="primary" class="ml_10" @click="()=>{getList()}" native-type="submit">搜索</el-button>
                <el-button @click="resetForm('sc')">重置</el-button>
            </el-form>
        </div>
        <el-table v-loading="tableload" :data="listData" row-key="id" style="width:100%" default-expand-all stripe border>
            <el-table-column label="姓名" prop="name"></el-table-column>
            <el-table-column label="机构" prop="organization"></el-table-column>
            <el-table-column label="研究领域" prop="domain"></el-table-column>
            <el-table-column label="技能特长" prop="skill"></el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button type="text" size="mini" @click="()=>{detail(scope.row)}">查看</el-button>
                    <el-button type="text" size="mini" @click="()=>{del(scope.row)}">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
            background
            layout="prev, pager, next, jumper"
            :total="listLen"
            :page-size="limit"
            @current-change="toPage"
            :current-page.sync="currentPage"
            class="txt_r mt_20"
        ></el-pagination>
        <!-- 表单 -->
        <el-dialog :title="iptForm?'新增':'详情'" :visible.sync="showForm" width="1100px" :close-on-click-modal="false">
            <el-form
                :model="detailData"
                label-position="right"
                :rules="formRules"
                ref="word"
                label-width="80px"
            >
                <el-form-item label="头像" prop="avatar">
                    <el-upload
                        :action="BASE_URL+'/admin/file/upload/createfile'"
                        name="file"
                        :before-upload="upimgBefore"
                        :on-success="upimgSuccess2"
                        :on-error="upimgError"
                        :show-file-list="false"
                        :disabled="!iptForm"
                    >
                        <el-button v-if="!detailData.avatar" size="small" type="primary" ref="btn">点击上传</el-button>
                        <img v-else :src="BASE_URL + '/' + detailData.avatar" class="form-img" />
                    </el-upload>
                </el-form-item>
                <el-form-item label="姓名" prop="name">
                    <el-input type="text" :disabled="!iptForm" v-model="detailData.name" placeholder="请输入姓名"></el-input>
                </el-form-item>
                <el-form-item label="机构名" prop="organization">
                    <el-input type="text" :disabled="!iptForm" v-model="detailData.organization" placeholder="请输入机构名"></el-input>
                </el-form-item>
                <el-form-item label="技能特长" prop="skill">
                    <el-input type="text" :disabled="!iptForm" v-model="detailData.skill" placeholder="请输入技能特长"></el-input>
                </el-form-item>
                <el-form-item label="研究领域" prop="domain">
                    <el-input type="text" :disabled="!iptForm" v-model="detailData.domain" placeholder="请输入研究领域"></el-input>
                </el-form-item>
                <el-form-item label="简介" prop="intro">
                    <el-input type="textarea" :disabled="!iptForm" :rows="3" v-model="detailData.intro" placeholder="请输入简介"></el-input>
                </el-form-item>
                <el-form-item label="成果描述" prop="achievement">
                    <el-input type="textarea" :disabled="!iptForm" :rows="3" v-model="detailData.achievement" placeholder="请输入成果描述"></el-input>
                </el-form-item>
                <el-form-item label="研究领域" prop="research">
                    <el-input type="textarea" :disabled="!iptForm" :rows="3" v-model="detailData.research" placeholder="请输入研究领域"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="showForm = false">取 消</el-button>
                <el-button v-if="iptForm" type="primary" @click="submit">发 布</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { BASE_URL } from "../../config/app";
import * as apis from "../../api/thinktank/thinktank";

export default {
    data() {
        return {
            tableload:false,
            upimgLoding:false,
            listData:[],
            search:{name:""},
            // 分页            
            listLen: 0,
            limit: 14,
            currentPage: 1,
            // 详情
            iptForm: true,
            detailData: {},
            showForm: false,
            BASE_URL: "",
            formRules: {
                name: [{required: true,message: "请输入名称",trigger: "blur"}],
                organization: [{required: true,message: "请输入机构名称",trigger: "blur"}],
                skill: [{required: true,message: "请输入技能特长",trigger: "blur"}],
                domain: [{required: true,message: "请输入研究领域",trigger: "blur"}],
                intro: [{required: true,message: "请输入简介",trigger: "blur"}],
                achievement: [{required: true,message: "请输入成果描述",trigger: "blur"}],
                research: [{required: true,message: "请输入研究领域",trigger: "blur"}]
            }
        };
    },
    created(){
        this.BASE_URL = BASE_URL;
        this.getList();
    },
    methods: {
        getList(page=1){
            this.tableload = true;
            apis.getList({
                ...this.search,
                page: page,
                per_page: this.limit
            }).then((data)=>{
                this.tableload = false;
                if(data.code){
                    this.$message.error(data.message);
                    return;
                }
                this.listLen = data.data.total;
                this.limit = data.data.per_page;
                this.listData = data.data.data;
            }).catch(()=>{
                this.tableload = false;
                this.$message({type: "error",message: "请求失败",duration: 1800});
            });
        },
        toPage(val){
            this.currentPage = val;
            this.getList(val);
        },
        del(row){
            this.$confirm("确定删除该内容吗", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(()=>{
                apis.del({id:row.id}).then((data)=>{
                    if(data.code){
                        this.$message.error(data.message);
                        return;
                    }
                    this.$message.success("删除成功");
                    this.getList(this.currentPage);
                }).catch(()=>{
                    this.$message.error("删除失败");
                })
            }).catch(()=>{});
        },
        detail(row){
            this.iptForm = false;
            this.detailData = row;
            this.showForm = true;
        },
        resetForm(name){
            this.$refs[name].resetFields();
        },
        // 插入图片
        upimgBefore(){ this.upimgLoding = true; },
        upimgSuccess2(data){
            this.upimgLoding = false;
            if (data.code) {this.$message.error('图片插入失败');return false;}
            this.detailData.avatar = data.data.key;
        },
        upimgError(){
            this.upimgLoding = false;
            this.$message.error('图片插入失败');
        },
        // 发布
        openWord(){
            this.iptForm = true;
            this.detailData = {};
            this.showForm = true;
        },
        submit(){
            if(!this.detailData.avatar){this.$message.error("请上传头像");return;}
            this.$refs['word'].validate((valid) => {
                if (!valid) {return false;}
                apis.add(this.detailData).then((data)=>{
                    if(data.code){
                        this.$message.error(data.message);
                        return false;
                    }
                    this.$message.success("发布成功");
                    this.showForm = false;
                    this.getList();
                }).catch(()=>{
                    this.$message.error("发布失败");
                })
            });
        }
    },
    components: {
    }
};
</script>

<style scoped>
.continer{padding-bottom:62px;}
.editor{height:400px; color:#333; line-height:1.42;}
.dn{display:none;}
.w_100{width:100%;}

.ml_10{margin-left:10px;}
.txt_r {text-align: right;}
.topbox {margin-bottom: 20px;}
.detailTable{font-size:16px;}
.detailTable td{padding:4px 8px;}
.detailTable .tr{text-align:right;}
.topbox{display:flex; align-items:center; justify-content: space-between;}
.topbox .el-form-item{margin-bottom:0;}

.quill_pb{padding-bottom:46px;}
.tagipt{display:inline-block; padding:0 15px; width: 114px; height:30px; line-height:1; border:1px solid #DCDFE6; border-radius:16px; color:#606266;}
.tag_txt{display:inline-block; margin-right:10px; height:30px; line-height:30px; padding:0 10px 0 15px; border-radius:16px; background:#409EFF; color:#fff;}
.tag_txt>*{vertical-align:middle;}
.tag_txt .close{cursor:pointer; padding-left:10px;}
.form-img{width:50px; height:50px; border-radius:50%;}

.word{padding:20px;}
.word .title{font-size:22px; text-align: center;}
.word .info{display:flex; justify-content: space-between; align-items: center; font-size:16px; color:#777777;}
.word .info .infoafter span{padding-left:2em;}
</style>
